/*
 *      lcm.h
 *      
 *      Copyright 2009 Daniele Ricci <denn86@gmail.com>, Luca Saccagi <luksak86@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include <vortex.h>
#ifndef _WIN32
#include <stdbool.h>
#include <sys/types.h>
#include <sys/dir.h>
#else
#include <time.h>
#include <assert.h>
#include <dirent.h>
#include <vortex_win32.h>
#include <winf.h>
#endif

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include "configuration.h"
#include "debug.h"

#ifndef _LCM
#define _LCM

#define _VERSION "dilca - Logical Correlation Module (v0.0.6)"
#define _VERSION1 "0.0.6"

#ifdef _WIN32
#define snprintf sprintf_s 
#define usleep Sleep
#define strncasecmp _strnicmp
#endif

#define PLAIN_PROFILE "http://fact.aspl.es/profiles/plain_profile" 
#define TLS_PROFILE "http://iana.org/beep/TLS"
#define VORTEX_SASL_CRAM_MD5 "http://iana.org/beep/SASL/CRAM-MD5"
#define MAX_SIGN 10000

/*ADDED*/ #ifdef _WIN32
/*ADDED*/ #define DEFAULT_CONF_FILE "C:\\idmp.conf"
/*ADDED*/ #define MOVECMD "move "
/*ADDED*/ #define RMCMD "del "
/*ADDED*/ #define STOREDIR "C:\\Documents and Settings\\Administrator\\Desktop\\DILCA\\dataset\\react\\\0"
/*ADDED*/ #define SENDDIR "C:\\Documents and Settings\\Administrator\\Desktop\\DILCA\\dataset\\hpsign_react\\\0"
/*ADDED*/ #define HP_DIR "C:\\Documents and Settings\\Administrator\\Desktop\\DILCA\\dataset\\hp_sign\\\0"
/*ADDED*/ #define NORMAL_DIR "C:\\Documents and Settings\\Administrator\\Desktop\\DILCA\\dataset\\sign\\\0"
/*ADDED*/ #define GROUPS_FILE "C:\\Documents and Settings\\Administrator\\Desktop\\DILCA\\ILCE\\LCM\\groups.conf"
/*ADDED*/ #define SIGN_DTD "C:\\Documents and Settings\\Administrator\\Desktop\\DILCA\\ILCE\\LCM\\crOink_sign.dtd"
/*ADDED*/ #else
/*ADDED*/ #define DEFAULT_CONF_FILE "/etc/idmp.conf\0"
/*ADDED*/ #define MOVECMD "mv "
/*ADDED*/ #define RMCMD "rm "
#define STOREDIR "../dataset/react/\0"
#define SENDDIR "../dataset/hpsign_react/\0"
#define HP_DIR "../dataset/hp_sign/\0"
#define NORMAL_DIR "../dataset/sign/\0"
#define GROUPS_FILE "../etc/crOink/groups.conf\0"
#define SIGN_DTD "crOink_sign.dtd"
/*ADDED*/ #endif



#define MAX_HASH_ENTRY 100
#define HASH_INDEX_LENGHT 2 //log10(MAX_HASH_ENTRY)
#define PARSE_DO 1
#define PARSE_UNDO 2


#define EXT_NET "128.1.2.3\0"
#define LAN "192.168.1.0\0"

typedef struct _groups{
	char *group_name;
	struct _groups *next;
}groups;

typedef struct _sign{
	int curstep;
	char *sig_id;
	long int timeout;
	char *on_timeout;
	char *severity;
	char *query_string;
	int undo_time;
	char *origin;
	groups *detect_group_list;
	groups *react_group_list;
	char *react_do;
	char *react_do_on_fail;
	char *react_undo;
	char *react_undo_on_fail;
	struct timeval timeout_start;
}sign;

/*ADDED*/conf *c0;
/*ADDED*/xmlValidCtxtPtr vctxt;
/*ADDED*/xmlDtdPtr pdtd;
/*ADDED*/sign *cur_sign;
/*DELETED*///sign cur_sign;
char *query2send;
groups *conf_g;
int query_responses_cnt;
int filenum;
bool proceed;

xmlHashTable *result;
xmlHashTable *env;
xmlHashTable *var;
xmlHashTable *react_do;
xmlHashTable *react_undo;

void version(int argc, char **argv);
void init(void);
void idmp_connection(VortexConnection *cn,VortexChannel *ch,VortexAsyncQueue *queue);
void vortex_error(VortexConnection *cn,VortexChannel *ch);
char *sasl_get_data(char *item);
void sasl_verify(VortexConnection *cn);
xmlDocPtr sign_dir_polling(const char *dirname,char *filename);
void HashCreate(void *payload,void *data,xmlChar *name);
void hash_deallocate(void *payload,xmlChar* name);
/*MODIF*///void hash_deallocate(void *payload,void *data,xmlChar* name);
void free_hash_tables(void);
/*ADDED*/void hash_free(void *payload,xmlChar* name);
//bool xmlDoc_validate(xmlDocPtr doc,const char *dtd);
/*MODIF*/bool xmlDoc_validate(xmlDocPtr doc);
int sign_get_data(xmlDocPtr doc);
void sign_handler(VortexChannel *ch,xmlDocPtr doc);
void select_handler(char *select);
void where_handler(char *where);
void idmp_query(VortexConnection *cn,VortexChannel *ch);
int results_handler(VortexChannel *ch,VortexConnection *cn,VortexAsyncQueue *queue,xmlDocPtr doc);
void react_handler(char *react_str,int do_undo);
void react_create(char *react_index,char *react_str,char t);
void react_save(char *react,char t);
void save_hp_sign(xmlDocPtr doc);
void free_sign_struct(bool total);
void free_structs(xmlDocPtr doc);
/*ADDED*/void xmlDTD_init(const char *dtd);
int timeout_handler(sign *cur_sign);
int timeout_start(sign *cur_sign);

#ifdef _WIN32
int win_gettimeofday(struct timeval *tv,void *notUsed);
#endif
#endif
